package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.oms.ValidateUserSafeReq;
import com.homedo.odin.solution.model.request.pc.PcUserSafeRequestBean;
import com.homedo.odin.solution.service.UserSafeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liyi on 2018/5/22.
 */
@Api(description = "白名单", tags = {"UserSafe"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/user_info")
public class UserSafeController {

    @Autowired
    UserSafeServiceImpl service;

    @ApiOperation(value = " PC申请白名单", httpMethod = "POST")
    @PostMapping(value = "/safes/{bizCode}")
    public ResultData userSafeApply(@Valid @RequestBody PcUserSafeRequestBean requestBean,
            @ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable("bizCode") String bizCode,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        boolean flag = service.userSafeApply(requestBean, bizCode);
        return ResultData.newInstance(Meta.CODE_200, flag);
    }


    @ApiOperation(value = "PC_前端校验用户白名单", httpMethod = "GET")
    @GetMapping(value = "/safes/{bizCode}")
    public ResultData validateUserSafe(@Valid @ModelAttribute ValidateUserSafeReq requestBean,
            @ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable("bizCode") String bizCode) {
        boolean isUserSafe = service.validateUserSafe(requestBean, bizCode);
        return ResultData.newInstance(Meta.CODE_200, isUserSafe);
    }

}
